
function songWin()
{
   try{
       
    
   // intel.xdk.player.stopAudio();
   // intel.xdk.player.playSound("http://149.210.188.153/hexar/Files/songs/win.ogg");
       document.getElementById('battle').pause();
       document.getElementById('win').play();
       
   }
    catch(e)
    {
    }
}



function songLost()
{
    try{
        
   //intel.xdk.player.stopAudio();
    //intel.xdk.player.playSound("http://149.210.188.153/hexar/Files/songs/lost.ogg");
        document.getElementById('battle').pause();
        document.getElementById('lost').play();
        
    }catch(e)
    {
    }
}

function songGame()
{
    try{
        
    document.getElementById('game').play();
   // intel.xdk.player.startAudio("http://149.210.188.153/hexar/Files/songs/game.ogg", true);
    }catch(e)
    {
    }
}

function songBattle()
{
    try{
      //  intel.xdk.player.startAudio("http://149.210.188.153/hexar/Files/songs/battle.ogg", true);
        document.getElementById('battle').play();
        
    }catch(e)
    {
        
        console.log(e)
    }
    
}



var end = false,
	timeout,
	result1 = [],
	result2 = [];
    
         
       
var returnResult = [];
    

function dice(player1, player2 ){
    end = false;
    result1 = [];
    result2 = [];     
     
    document.getElementById('player1').innerHTML = player1.name;
    document.getElementById('player2').innerHTML = player2.name;
    document.getElementById('p1units').innerHTML = player1.units;
    document.getElementById('p2units').innerHTML = player2.units;
    document.getElementById('dice1').src = 'img/dice/Logo.png';
    document.getElementById('dice2').src = 'img/dice/Logo.png';
    
    document.getElementById("list1").innerHTML = "";    
    document.getElementById("list2").innerHTML = "";
    setTimeout(function(){roll(player1, player2);},2000);
}

//Function that changes the image of the dice
//Has nmb for specific image change
//When there's no nmb it start the randomized animation
//The result of this is negligible
function changeImg(id,nmb){
	var dice = document.getElementById(id);
	if(nmb){
		dice.src = 'img/dice/' + nmb + '.png';
	}
	else{
		var rnd = Math.floor((Math.random() * 20) + 1);
		dice.src = 'img/dice/' + rnd + '.png';
		timeout = setTimeout(function(){ changeImg(id);},40);
		setTimeout(function(){ clearTimeout(timeout);},7000);
	}
}


function addResult(id, result, units, color){
	var node = document.createElement('li'),
		//img = document.createElement('img');
		txt = document.createTextNode(result);
    
    node.style.color= color;
    node.appendChild(txt);
    
	if(id == 'dice1'){
		document.getElementById("list1").appendChild(node);
		document.getElementById('p1units').innerHTML = units;
	}
	else if(id == 'dice2'){
		document.getElementById("list2").appendChild(node);
		document.getElementById('p2units').innerHTML = units;
	}
}

//Function that gets initiated when the button is clicked
function roll(player1, player2){
	if(player1.units > 1 && player2.units > 0){
		var i = 0,
			j = 0,
			interval,
			interval2,
            p1color,
            p2color;
		while(player1.units > 0 && player2.units >= 0){
			var p1dice = 0, 
				p2dice = 0;	
			
			if(end){
				changeImg('dice1');
				changeImg('dice2');
				
				interval = setInterval(function(){
					addResult('dice1', result1[i].result, result1[i].units, result1[i].color);
					i++;
					if(i == result1.length){
						clearInterval(interval);
					}
				}, 7000/result1.length);
				
				interval2 = setInterval(function(){
					addResult('dice2', result2[j].result, result2[j].units, result2[j].color);
					j++;
					if(j == result2.length){
						clearInterval(interval2);
					}
				}, 7000/result2.length);
				
				setTimeout(function(){
					if(player2.units == 0){
                        //alert('player 2 lost');   
                        changeImg('dice1', 'winner');
                        changeImg('dice2', 'loser');
                        songWin();
                        
                        
                    }
					if(player1.units == 1){
                        //alert("player 1 can't attack anymore");
                        changeImg('dice1', 'loser');
                        changeImg('dice2', 'loser');
                        songLost();
                    }
                    
                    setTimeout(function(){  $( "#popUpDice" ).dialog( "close" );}, 3000);
                    
				},7100);
				
				break;
			}
			else if(player2.units == 0)
			{
				end = true;
			}
			else if(player1.units == 1)
			{
				end = true;
			}
			else{
				p1dice = Math.floor((Math.random() * 20) + 1); //random value
				p2dice = Math.floor((Math.random() * 20) + 1); //random value				
				
				//Player 1 lost and loses a point
				//Show the result of roll
				//Show the remaining points
				if(p1dice < p2dice){
					player1.units--;
                    p1color = "red";
                    p2color = "green";
				}
				//Player 2 lost and loses a point
				//Show the result of roll 
				//Show the remaining points
				else if(p1dice > p2dice){
					player2.units--;
                    p1color = "green";
                    p2color = "red";
				}
				//It's a tie, both players lose a point
				//Show the result of roll 
				//Show the remaining points
				else if(p1dice == p2dice){
					player1.units--;
					player2.units--;
                    p1color = "red";
                    p2color = "red";
				}
				
				result1[result1.length] = {result: p1dice, units: player1.units, color: p1color};
				result2[result2.length] = {result: p2dice, units: player2.units, color: p2color};
			}
		}
	}
	else if(player1.units == 1){
        
          returnResult[0] = player1;
        returnResult[1] = player2;
       
       
        
       
		
	}
	else if(player2.units == 0){
        
       returnResult[0] = player1;
        returnResult[1] = player2;
        
      
	}
    
        returnResult[0] = player1;
        returnResult[1] = player2;
        
      
        
}